﻿
using Microsoft.VisualBasic;
using OpenHttpServer;
using OpenHttpServer.Models;
using System;
using System.Collections.Generic;

using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;


namespace VIID.Server
{
    public class System:Server
    {
        /// <summary>
        /// 注册接口
        /// </summary>
        /// <param name="DeviceID">设备ID</param>
        /// <returns></returns>
        public static HttpResponse Register(HttpRequest request)
        {
            if (!request.Headers.ContainsKey("User-Identify"))
                return HttpBuilder.Unauthorized();

            if (!request.Headers.ContainsKey("Authenticate") && (_options.authMode == 1))//没有提交认证值
            {
                var _response = new HttpResponse()
                {
                    StatusCode = 401,
                    ContentAsUTF8 = "认证信息不足"
                };
                _response.Headers.Add("WWW-Authenticate", string.Format("Digest realm=\"{0}\",qop=\"auth\",nonce=\"{1}\",opaque=\"{2}\"", _options.realm, Guid.NewGuid().ToString(), _options.realm));
                return _response;
                //header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
            }

            var Identify = request.Headers["User-Identify"];
            var response = messageCallback.Invoke(Identify, VIID.COMMAND_SYSTEM_REGISTER_POST, request);

            return response;
        }
        /// <summary>
        /// 注销端口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static HttpResponse UnRegister(HttpRequest request)
        {
            if (!request.Headers.ContainsKey("User-Identify"))
                return HttpBuilder.Unauthorized();

            var Identify = request.Headers["User-Identify"];

            var response = messageCallback.Invoke(Identify, VIID.COMMAND_SYSTEM_UNREGISTER_POST, request);


            return response;


        }


        /// <summary>
        /// 保活接口
        /// </summary>
        /// <param name="DeviceID"></param>
        /// <returns></returns>
        public static HttpResponse Keepalive(HttpRequest request)
        {
            if (!request.Headers.ContainsKey("User-Identify"))
                return HttpBuilder.Unauthorized();

            var Identify = request.Headers["User-Identify"];

            var response = messageCallback.Invoke(Identify, VIID.COMMAND_SYSTEM_KEEPALIVE_POST, request);


            return response;



        }
        /// <summary>
        /// 校时接口
        /// </summary>
        /// <returns></returns>
        public static HttpResponse Time(HttpRequest request)

        {

            if (!request.Headers.ContainsKey("User-Identify"))
                return HttpBuilder.Unauthorized();

            var Identify = request.Headers["User-Identify"];

            var response = messageCallback.Invoke(Identify, VIID.COMMAND_SYSTEM_TIME_GET, null);


            return response;


        }
    }
}
